home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 3 of 3.iso / chapte19 / cdaudio.c next >
C/C++ Source or Header  |  1996-04-29  |  7KB  |  237 lines

  1.  
  2. #include <windows.h>
  3. #include <stdio.h>
  4. #include "CDAudio.h"
  5. #include <vfw.h>
  6.  
  7. #if defined (WIN32)
  8.     #define IS_WIN32 TRUE
  9. #else
  10.     #define IS_WIN32 FALSE
  11. #endif
  12.  
  13. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  14. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  15. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  16.  
  17. HINSTANCE hInst;   // current instance
  18.  
  19. LPCTSTR lpszAppName = "MyApp";
  20. LPCTSTR lpszTitle   = "My Application"; 
  21.  
  22. // the rest of the stuff
  23. //......................
  24.  
  25. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  26.  
  27.  
  28. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  29.                       LPTSTR lpCmdLine, int nCmdShow)
  30. {
  31.    MSG      msg;
  32.    HWND     hWnd; 
  33.    WNDCLASS wc;
  34.  
  35.    wc.style         = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  36.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  37.    wc.cbClsExtra    = 0;                      
  38.    wc.cbWndExtra    = 0;                      
  39.    wc.hInstance     = hInstance;              
  40.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  41.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  42.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  43.    wc.lpszMenuName  = lpszAppName;              
  44.    wc.lpszClassName = lpszAppName;              
  45.  
  46.    if ( IS_WIN95 )
  47.    {
  48.       if ( !RegisterWin95( &wc ) )
  49.          return( FALSE );
  50.    }
  51.    else if ( !RegisterClass( &wc ) )
  52.       return( FALSE );
  53.  
  54.    hInst = hInstance; 
  55.  
  56.    hWnd = CreateWindow( lpszAppName, 
  57.                         lpszTitle,    
  58.                         WS_OVERLAPPEDWINDOW, 
  59.                         CW_USEDEFAULT, 0, 
  60.                         CW_USEDEFAULT, 0,  
  61.                         NULL,              
  62.                         NULL,              
  63.                         hInstance,         
  64.                         NULL               
  65.                       );
  66.  
  67.    if ( !hWnd ) 
  68.       return( FALSE );
  69.  
  70.    ShowWindow( hWnd, nCmdShow ); 
  71.    UpdateWindow( hWnd );         
  72.  
  73.    while( GetMessage( &msg, NULL, 0, 0) )   
  74.    {
  75.       TranslateMessage( &msg ); 
  76.       DispatchMessage( &msg );  
  77.    }
  78.  
  79.    return( msg.wParam ); 
  80. }
  81.  
  82.  
  83. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  84. {
  85.     WNDCLASSEX wcex;
  86.  
  87.    wcex.style         = lpwc->style;
  88.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  89.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  90.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  91.    wcex.hInstance     = lpwc->hInstance;
  92.    wcex.hIcon         = lpwc->hIcon;
  93.    wcex.hCursor       = lpwc->hCursor;
  94.    wcex.hbrBackground = lpwc->hbrBackground;
  95.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  96.    wcex.lpszClassName = lpwc->lpszClassName;
  97.  
  98.    // Added elements for Windows 95.
  99.    //...............................
  100.    wcex.cbSize = sizeof(WNDCLASSEX);
  101.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  102.                             IMAGE_ICON, 16, 16,
  103.                             LR_DEFAULTCOLOR );
  104.             
  105.    return RegisterClassEx( &wcex );
  106. }
  107.  
  108. HWND       hMCIWnd  = NULL;   // MCIWnd window handle
  109.  
  110.  
  111. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  112. {
  113.    switch( uMsg )
  114.    {
  115.       case WM_CREATE :
  116.               // create MCIWnd window
  117.               //.....................
  118.  
  119.               hMCIWnd = MCIWndCreate(hWnd, hInst, 
  120.                                      WS_VISIBLE | 
  121.                                      WS_CHILD | 
  122.                                      WS_OVERLAPPED |
  123.                                      WS_CAPTION |
  124.                                      WS_BORDER |
  125.                                      MCIWNDF_NOTIFYMODE |
  126.                                      MCIWNDF_SHOWALL | 
  127.                                      MCIWNDF_NOPLAYBAR, 
  128.                                      NULL
  129.                                     );
  130.  
  131.               if (hMCIWnd)
  132.                   MCIWndOpen(hMCIWnd, "cdaudio", 0);
  133.               else            
  134.               {
  135.                   MessageBox(hWnd, "Error creating MCIWnd window...", 
  136.                              NULL, MB_OK);
  137.                   DestroyWindow( hWnd );
  138.               }
  139.               break;
  140.  
  141.       case WM_COMMAND :
  142.               switch( LOWORD( wParam ) )
  143.               {
  144.                  // eject
  145.                  //......
  146.  
  147.                  case IDM_EJECT :
  148.                          if ( MCIWndCanEject(hMCIWnd) )
  149.                          {
  150.                              MCIWndStop(hMCIWnd);
  151.                              MCIWndEject(hMCIWnd);
  152.                          }
  153.                          break;
  154.  
  155.                  // playback
  156.                  //.........
  157.                                            
  158.                  case IDM_PLAY :
  159.                          {
  160.                              LONG lMode = MCIWndGetMode(hMCIWnd, NULL, 0);
  161.                              
  162.                              if ( !(lMode & MCI_MODE_OPEN) ||
  163.                                   (lMode & MCI_MODE_NOT_READY) )
  164.                                  MCIWndOpen(hMCIWnd, "cdaudio", 0);
  165.  
  166.                              MCIWndPlay(hMCIWnd);
  167.                          }
  168.                          break;
  169.                                    
  170.                  case IDM_STOP :
  171.                          MCIWndStop(hMCIWnd);
  172.                          break;
  173.  
  174.                  // other commands
  175.                  //...............
  176.                  
  177.                  case IDM_ABOUT :
  178.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  179.                         break;
  180.  
  181.                  case IDM_EXIT :
  182.                         DestroyWindow(hWnd);
  183.                         break;
  184.               }
  185.               break;
  186.  
  187.       case WM_DESTROY :
  188.               // destroy MCIWnd, if one exists
  189.               //..............................
  190.  
  191.               if (hMCIWnd)
  192.                   MCIWndDestroy(hMCIWnd);
  193.  
  194.               PostQuitMessage(0);
  195.               break;
  196.  
  197.       default :
  198.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  199.    }
  200.  
  201.    return( 0L );               
  202. }
  203.  
  204.  
  205. LRESULT CALLBACK About( HWND hDlg,           
  206.                         UINT message,        
  207.                         WPARAM wParam,       
  208.                         LPARAM lParam)
  209. {
  210.    switch (message) 
  211.    {
  212.        case WM_INITDIALOG: 
  213.                return (TRUE);
  214.  
  215.        case WM_COMMAND:                              
  216.                if (   LOWORD(wParam) == IDOK         
  217.                    || LOWORD(wParam) == IDCANCEL)    
  218.                {
  219.                        EndDialog(hDlg, TRUE);        
  220.                        return (TRUE);
  221.                }
  222.                break;
  223.    }
  224.  
  225.    return (FALSE); 
  226. }
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.